//
//  WeatherIndexViewController.m
//  MINFOBBS
//
//  Created by 蔡龙生 on 13-11-22.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "WeatherIndexViewController.h"
#import "SBJsonParser.h"
#import "DetectionSystemVersion.h"
#import "UIColor+expanded.h"
#import "MainSearchViewController.h"
@interface WeatherIndexViewController ()

@end

@implementation WeatherIndexViewController
@synthesize city;
@synthesize query;



- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    if (self.isSelf) {
        self.table.frame=CGRectMake(self.table.frame.origin.x, 0, self.table.frame.size.width, self.table.frame.size.height);
//         NSLog(@"%@",self.arrReturn);
        [self SetCellHight];
       
    }
    else{
        if ([[DetectionSystemVersion alloc]Version]) {
            self.edgesForExtendedLayout=UIRectEdgeNone;
        }
        self.table.layer.cornerRadius=8;
        self.table.showsVerticalScrollIndicator=YES;
        self.table.bounces=YES;
        [self BackButton];
        if (self.isNoNet) {
            [self SetCellHight];

        }
        else{
        [self GetWeatherSearch:self.query Location:self.city];
    }

    }
   }
- (void)viewWillDisappear:(BOOL)animated{
    [tRequest clearDelegatesAndCancel];
    [tRequest cancel];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [NSThread cancelPreviousPerformRequestsWithTarget:self];
}

-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
    
}
#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Get WeatherIndex Search Data 天气指数
-(void)GetWeatherSearch:(NSString*)Query Location:(NSString*)City{
 tRequest=tRequest=[[[ScreenAccess alloc]init]ASIGetSearch:Query Start:0 Limit:10];      [tRequest setDelegate:self];
    [tRequest setUsername:@"weatherIndex"];
}

-(void)requestFinished:(ASIHTTPRequest*)request{
    NSString *response=[request responseString];
    SBJsonParser *parse=[[SBJsonParser alloc] init];
    NSMutableDictionary *Result=[parse objectWithString:response];
    if([request.username isEqual:@"weatherIndex"]){
        BOOL IsSuccess=[[[Result objectForKey:@"Response"] objectForKey:@"status"] integerValue]==200?YES:NO;
        if(IsSuccess==YES){
            NSMutableArray *arrReturn=[[NSMutableArray alloc] init];
            [arrReturn addObject:[[Result objectForKey:@"Response"] objectForKey:@"Categories"]];
            for (int i=0; i<arrReturn.count; i++) {
                NSArray *arr=[[arrReturn objectAtIndex:i] objectForKey:@"Category"];
                for (int j=0; j<arr.count; j++) {
                    if([[[arr objectAtIndex:j] objectForKey:@"Entrance"] isEqual:@"Weather.GetWeatherPoints"]){
                            if(self.arrReturn==nil){
                                self.arrReturn=[[NSMutableArray alloc] init];
                            }
                            self.arrReturn=[[[[[[arr objectAtIndex:j] objectForKey:@"Items"] objectForKey:@"Item"]objectAtIndex:0] objectForKey:@"Properties"] objectForKey:@"Property"];
                        }
                }
            }
            [self SetCellHight];
        }
           }
}

-(void)requestFailed:(ASIHTTPRequest*)request{
    NSLog(@"呵呵");
}
-(void)SetCellHight{
    CGSize sizeFrame;
    arrRowHight=[[NSMutableArray alloc]init];
    int hight=2;
    if (self.isSelf) {
        hight=1;
    }
    for (int i=hight; i<self.arrReturn.count; i++) {
        NSString *str;
            str=[[self.arrReturn objectAtIndex:i]objectForKey:@"text"];

        if (str.length!=0) {
            sizeFrame =[str sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(280, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
            if (i==1) {
                [arrRowHight addObject:[NSString stringWithFormat:@"%f",sizeFrame.height+20]];
 
            }
            else{
                [arrRowHight addObject:[NSString stringWithFormat:@"%f",sizeFrame.height+30]];
 
            }
                   }
    }
    [self.table reloadData];
//    loadMore.frame=CGRectMake(0, self.tableView.contentSize.height-35, 320, 32);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSArray *list=[[[weatherIndex objectAtIndex:0] objectForKey:@"Properties"] objectForKey:@"Property"];
    if(self.isSelf>0){
        return 3;
    }
    else{
        return self.arrReturn.count-2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor clearColor];
    }
    if (self.isSelf) {
        if (indexPath.row==0) {
            cell.textLabel.text=[NSString stringWithFormat:@"%@                         %@",[[self.arrReturn objectAtIndex:indexPath.row] objectForKey:@"text"],[[self.arrReturn objectAtIndex:indexPath.row+1] objectForKey:@"text"]];
            cell.textLabel.textColor=[UIColor grayColor];
            cell.indentationLevel = 1;
            cell.indentationWidth = 28.0f;

        }
        else{
            cell.textLabel.text=[[self.arrReturn objectAtIndex:indexPath.row+1] objectForKey:@"name"];
            cell.detailTextLabel.text=[[self.arrReturn objectAtIndex:indexPath.row+1] objectForKey:@"text"];
                       cell.imageView.image=[UIImage imageNamed:[[NSString alloc] initWithFormat:@"zs_%d",indexPath.row+1]];
        }
    }
    else{
        NSUInteger row=[indexPath row];
        cell.textLabel.text=[[self.arrReturn objectAtIndex:row+2] objectForKey:@"name"];
      
        cell.imageView.image=[UIImage imageNamed:[[NSString alloc] initWithFormat:@"zs_%d",row]];
        cell.detailTextLabel.text=[[self.arrReturn objectAtIndex:row+2] objectForKey:@"text"];
       

    }
      cell.textLabel.font=[UIFont systemFontOfSize:15];
    cell.detailTextLabel.font=[UIFont systemFontOfSize:14];
    cell.detailTextLabel.textColor=[UIColor colorWithHexString:@"0x202020"];
    cell.detailTextLabel.numberOfLines=0;
        return cell;
}
-(float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.isSelf) {
         return 38;
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *result;
    result=[[UIView alloc]init];
    result.backgroundColor=[UIColor clearColor];
    if (self.isSelf) {
        UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.table.frame.size.width, 3)];
        img.image=[UIImage imageNamed:@"footLine"];
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 3, self.table.frame.size.width, 35)];
        [btn setTitle:@"点击全部指数" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(PrseeMore) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:[UIColor colorWithHexString:@"0xb7b7b7"] forState:UIControlStateNormal];
        btn.titleLabel.font=[UIFont systemFontOfSize:14];
        [result addSubview:btn];
        [result addSubview:img];
    }
    return result;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [[arrRowHight objectAtIndex:indexPath.row] floatValue];
}
- (MainSearchViewController *)menuContainerViewController {
    return (MainSearchViewController *)self.parentViewController;
}
-(void)PrseeMore{
    //    if (self.isSelf) {
    WeatherIndexViewController *jokecon=[[WeatherIndexViewController alloc]init];
    jokecon.arrReturn=self.arrReturn;
    jokecon.isNoNet=YES;
    [ [self menuContainerViewController].navigationController  pushViewController:jokecon animated:YES];
    //    }
    
    
}
@end
